import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().reduce((previousValue, currentValue) => {
  const moduleName = currentValue.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(currentValue)
  previousValue[moduleName] = value.default
  return previousValue
}, {})

export default new Vuex.Store({
  modules,
  state: {},
  getters: {
    permission_routes: state => state.permission.routes,
    addRoutes: state => state.permission.addRoutes,
    menuRoutes: state => {
      const rootRoute = state.permission.routes.find(r => r.name.match(/root/))
      return rootRoute ? rootRoute.children : []
    },
    visitedViews: state => state.tagsView.visitedViews,
    cachedViews: state => state.tagsView.cachedViews,
    breakpoint: state => state.app.breakpoint,
    isLikeMobileScreen: state => state.app.breakpoint === 'xs',
    isLikeTabletScreen: state => ['lg', 'md', 'sm'].includes(state.app.breakpoint)
  }
})
